package com.yanqingshan.platform.module.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanqingshan.platform.module.sys.mapper.SysRoleResourceMapper;
import com.yanqingshan.platform.module.sys.model.domain.SysRoleResource;
import com.yanqingshan.platform.module.sys.service.SysRoleResourceService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:系统角色资源对象 Service业务层处理
 *
 * @author yanqs
 * @since 2023-05-15
 */
@Service
public class SysRoleResourceServiceImpl extends ServiceImpl<SysRoleResourceMapper, SysRoleResource>
        implements SysRoleResourceService {
    /**
     * 角色分配资源
     *
     * @param roleId 角色id
     * @param ids    资源ids
     */
    @Override
    public void grantResource(Long roleId, List<Long> ids) {
        // 删除角色 资源 重新赋值
        remove(new LambdaQueryWrapper<SysRoleResource>().eq(SysRoleResource::getRoleId, roleId));
        List<SysRoleResource> saveList = new ArrayList<>();
        for (Long id : ids) {
            SysRoleResource roleResource = new SysRoleResource();
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(id);
            saveList.add(roleResource);
        }
        saveBatch(saveList);
    }
}
